//
//  FlipsideViewController.m
//  iPhoneRegister
//
//  Created by Devon Buchanan on 27/June/2009.
//  Copyright divinenephron 2009. All rights reserved.
//
// TODO: Make this move the cell being edited to the middle of the screen.
// TODO: Make this save the text you're in the process of entering even when you quit by pressing the home button.

#import "FlipsideViewController.h"

@implementation FlipsideViewController

@synthesize delegate, dataSource, tableHeader;

#pragma mark -
#pragma mark Initialization and finishing

- (void)viewDidLoad {
    [super viewDidLoad];
    //    [self.tableView setBackground:[UIColor viewFlipsideBackgroundColor]];
    [[NSBundle mainBundle] loadNibNamed:@"FlipsideHeaderView" owner:self options:nil];
    self.tableView.tableHeaderView = self.tableHeader;
    self.tableHeader = nil;
}

- (IBAction)done {
	[self.delegate flipsideViewControllerDidFinish:self];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDelegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.
    return 2;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // The header for the section is the region name -- get this from the region at the section index.
    if (section == 0) {
        return @"NTLM Details";
    } else {
        return @"SMS Details";
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger currentFieldTag;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            currentFieldTag = kNTLMUsernameTag;
        } else { /* indexPath.row == 1 */
            currentFieldTag = kNTLMPasswordTag;
        }
    } else { /* indexPath.section == 1 */
        if (indexPath.row == 0) {
            currentFieldTag = kSMSUsernameTag;
        } else { /* indexPath.row == 1 */
            currentFieldTag = kSMSPasswordTag;
        }
    }
    
    return [self tableCellWithTag:currentFieldTag];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Start editing the cell's text field when you try to select a row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITextField *textField;
    if (textField = (UITextField *)[cell.contentView viewWithTag:kNTLMUsernameTag]) {
        ;
    } else if (textField = (UITextField *)[cell.contentView viewWithTag:kNTLMPasswordTag]) {
        ;
    } else if (textField = (UITextField *)[cell.contentView viewWithTag:kSMSUsernameTag]) {
        ;
    } else if (textField = (UITextField *)[cell.contentView viewWithTag:kSMSPasswordTag]) {
        ;
    } else {
        textField = nil;
    }
    [textField becomeFirstResponder];
    return nil;
}

#pragma mark -
#pragma mark Cell Creation Methods

- (UITableViewCell *)tableCellWithTag:(NSInteger)tag {
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UITextField *textField = [self textFieldForCellWithTag:tag];
    switch (tag) {
        case kNTLMUsernameTag:
            cell.textLabel.text = @"Username";
            textField.placeholder = @"example@org.local";
            break;
        case kSMSUsernameTag:
            cell.textLabel.text = @"Username";
            textField.placeholder = @"JohnnyAppleseed";
            break;
        case kNTLMPasswordTag:
        case kSMSPasswordTag:
            cell.textLabel.text = @"Password";
            textField.placeholder = @"Required";
            textField.secureTextEntry = YES;
            break;
        default:
            break;
    }
    [cell.contentView addSubview:textField];
    return cell;
}

- (UITextField *)textFieldForCellWithTag:(NSInteger)tag {
    UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(110, 10, 180, 22)] autorelease];
    // Don't autocorrect
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    // Make the field look right
    textField.textColor = [UIColor colorWithRed:0.220 green:0.333 blue:0.526 alpha:1.0];
    textField.adjustsFontSizeToFitWidth = YES;
    textField.minimumFontSize = 9.0;
    // Make this controller the delegate
    textField.delegate = self;
    // Fill in the field's text
    textField.text = [self textForTextFieldWithTag:tag];
    textField.tag = tag;
    return textField;
}

- (NSString *)textForTextFieldWithTag:(NSInteger)tag {
    switch (tag) {
        case kNTLMUsernameTag:
            return self.dataSource.NTLMUsername;
            break;
        case kSMSUsernameTag:
            return self.dataSource.SMSUsername;
            break;
        case kNTLMPasswordTag:
            return self.dataSource.NTLMPassword;
            break;
        case kSMSPasswordTag:
            return self.dataSource.SMSPassword;
            break;
        default:
            return nil;
            break;
    }
}

#pragma mark -
#pragma mark UITextFieldDelegate methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSInteger tag = textField.tag;
    if (tag != kSMSPasswordTag) {
        tag += 1;
        UIView *nextView = [self.view viewWithTag:tag];
        [nextView becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == kNTLMUsernameTag) {
        self.dataSource.NTLMUsername = textField.text;
        NSLog(@"%@", self.dataSource.NTLMUsername);
    } else if (textField.tag == kNTLMPasswordTag) {
        self.dataSource.NTLMPassword = textField.text;
        NSLog(@"%@", self.dataSource.NTLMPassword);
    } else if (textField.tag == kSMSUsernameTag) {
        self.dataSource.SMSUsername = textField.text;
        NSLog(@"%@", self.dataSource.SMSUsername);
    } else if (textField.tag == kSMSPasswordTag) {
        self.dataSource.SMSPassword = textField.text;
        NSLog(@"%@", self.dataSource.SMSPassword);
    } else {
        ;
    }
    return;
}


@end
